package com.ruoyi.system.utils;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.RepBase;
import com.ruoyi.system.domain.RepPro;
import com.ruoyi.system.domain.RepProList;
import com.ruoyi.system.service.IRepBaseService;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class ExcelImp {

    public static void main(String[] args) throws Exception {
//        print();
//        System.out.println(11111111);
    }

    /**
     * 打印方法
     * @throws Exception
     */
    public static AjaxResult print(List<RepBase> list, IRepBaseService repBaseService){
//        //准备数据
//        List<RepPro> list1 = new ArrayList<RepPro>();
//        for (int i = 0; i < 20; i++) {
//            RepPro stu = new RepPro();
//            stu.setId(i);
//            stu.setProductName("一年级");
//            stu.setDetail("张三" + i);
//            stu.setBrand("男");
//            list1.add(stu);
//        }
        //获取模板文件，你自己当前模板的位置  我这里为当前项目下
        //如果为系统路径 为： InputStream is = new FileInputStream("F://student.xlsx");
        InputStream is = null;
        try{
            is = new FileInputStream("tmsmodel.xlsx");
        }catch(Exception e) {
            e.printStackTrace();
        }
        //根据模板生成的文件保存路径  我这里保存在本地D盘
        String filename = encodingFilename("student");
        OutputStream os = null;
        try {
            os = new FileOutputStream(getAbsoluteFile(filename));
        }catch(Exception e) {
            e.printStackTrace();
        }
        //绑定数据
        Context context = new Context();

        RepBase repBase = list.get(0);
        context.putVar("clientCompany", repBase.getClientCompany());
        context.putVar("contactInfo", repBase.getContactInfo());
        context.putVar("address", repBase.getAddress());
        context.putVar("quotationDate", repBase.getQuotationDate());
        context.putVar("paymentMethod", repBase.getPaymentMethod());
        context.putVar("deliveryDate", repBase.getDeliveryDate());
        context.putVar("quaranteePerid", repBase.getQuaranteePerid());
        context.putVar("totalPrice", repBase.getTotalPrice());
        RepPro repPro = new RepPro();
        repPro.setOrderNum(repBase.getId());
        List<RepProList> list1 = repBaseService.selectRepProListForDisc(repPro);
//        List<RepPro> list122 = repBaseService.selectDetail(repBase.getId());

        context.putVar("list", list1);
        //生成
        try{
            JxlsHelper.getInstance().processTemplate(is, os, context);
        }catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.success(filename);
    }

    /**
     * 编码文件名
     */
    public static String encodingFilename(String filename)
    {
        filename = UUID.randomUUID().toString() + "_" + filename + ".xls";
        return filename;
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public static String getAbsoluteFile(String filename)
    {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

}

